package org.example.resourceplatform.service.impl;

import org.example.resourceplatform.entity.User;
import org.example.resourceplatform.repository.UserRepository;
import org.example.resourceplatform.service.UserService;
import org.example.resourceplatform.dto.LoginRequest;
import org.example.resourceplatform.dto.RegisterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Optional<User> login(LoginRequest loginRequest) {
        System.out.println("=== 用户服务层登录验证 ===");
        System.out.println("邮箱: " + loginRequest.getEmail());
        System.out.println("密码: " + loginRequest.getPassword());

        // 只通过邮箱查找用户
        Optional<User> userOptional = userRepository.findByEmail(loginRequest.getEmail());

        if (userOptional.isPresent()) {
            User user = userOptional.get();
            System.out.println("找到用户:");
            System.out.println("用户ID: " + user.getUserId());
            System.out.println("姓名: " + user.getName());
            System.out.println("邮箱: " + user.getEmail());
            System.out.println("数据库密码: " + user.getPassword());
            System.out.println("输入密码: " + loginRequest.getPassword());

            // 直接比较明文密码
            if (user.getPassword().equals(loginRequest.getPassword())) {
                System.out.println("密码匹配成功");
                System.out.println("=== 用户服务层登录验证结束 ===");
                return Optional.of(user);
            } else {
                System.out.println("密码不匹配");
            }
        } else {
            System.out.println("未找到匹配的用户");
        }

        System.out.println("=== 用户服务层登录验证结束 ===");
        return Optional.empty();
    }

    @Override
    public User register(RegisterRequest registerRequest) {
        // 检查邮箱是否已存在
        if (userRepository.existsByEmail(registerRequest.getEmail())) {
            throw new RuntimeException("邮箱已被注册");
        }

        // 创建新用户
        User user = new User();
        user.setEmail(registerRequest.getEmail());
        user.setPassword(registerRequest.getPassword()); // 明文存储
        user.setRole(registerRequest.getRole());
        user.setCreateTime(LocalDateTime.now());

        // 设置默认姓名（使用邮箱前缀）
        String name = registerRequest.getEmail().split("@")[0];
        user.setName(name);

        return userRepository.save(user);
    }

    @Override
    public Optional<User> findByEmail(String email) {
        return userRepository.findByEmail(email);
    }


    @Override
    public Optional<User> findByRole(String role) {
        return userRepository.findByRole(role);
    }

    @Override
    public boolean existsByEmail(String email) {
        return userRepository.existsByEmail(email);
    }

    @Override
    public User updateUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User cannot be null");
        }
        return userRepository.save(user);
    }

    @Override
    public void deleteUser(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("User ID cannot be null");
        }
        userRepository.deleteById(id);
    }
}